package com.ldj.Aop;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author Ldj
 * @Date 2018/2/23 11:39
 * @Description 数据处理
 */
public class CglibAop implements MethodInterceptor {

    private Object targetObject;

    public Object getProxyInstance(Object targetObject){
        this.targetObject = targetObject;
        //用于生成代理对象
        Enhancer enhancer = new Enhancer();
        //设置父类
        enhancer.setSuperclass(this.targetObject.getClass());
        //设置回调对象为本身
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        checkSecurity();
        return methodProxy.invoke(targetObject,objects);
    }

    //新增方法，需要满足的业务新需求。
    private void checkSecurity(){
        System.out.println("安全检查---CGLIB实现--");
    }
}
